package com.example.mybatisplusgenerator;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

public class CodeGenerator01 {

    /**
     * 选择模板引擎
     * <p>
     *     默认 velocity 引擎
     * </p>
     */
    @Test
    public void testMybatisplusGeneratorTemplateEngine() {
        AutoGenerator generator = new AutoGenerator();

// set freemarker engine
//        generator.setTemplateEngine(new FreemarkerTemplateEngine());

// set beetl engine
//        generator.setTemplateEngine(new BeetlTemplateEngine());

// set custom engine (reference class is your custom engine class)
//        generator.setTemplateEngine(new CustomTemplateEngine());
    }

    @Test
    public void testMybatisplusGenerator() {
        AutoGenerator generator = new AutoGenerator();

        // 输出目录
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
        globalConfig.setAuthor("jobob");
        globalConfig.setOpen(false);
        globalConfig.setOpen(true);

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/ant?useUnicode=true&useSSL=false&characterEncoding=utf8");
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("rootroot");
    }

    /**
     * 自定义属性注入
     */
    @Test
    public void test() {
        InjectionConfig injectionConfig = new InjectionConfig() {
            //自定义属性注入:abc
            //在.ftl(或者是.vm)模板中，通过${cfg.abc}获取属性
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<>();
                map.put("abc", this.getConfig().getGlobalConfig().getAuthor() + "-mp");
                this.setMap(map);
            }
        };
        AutoGenerator mpg = new AutoGenerator();
        //  配置自定义属性注入
        mpg.setCfg(injectionConfig);
    }
}
